<!DOCTYPE html>
<html>

<head>
  <title>成员列表</title>
  <% include layout/lib.ejs %>
  <link rel="stylesheet" type="text/css" href="/css/memberManager.css" />
  <script type="text/javascript" src="/lib/js/aliyun-oss-sdk-4.4.4.min.js"></script>
</head>

<body>
  <div class="pageTitleBox">
    <span id="nowTeam"></span> / 成员管理
  </div>
  <div class="memberManagerTitle">新增成员</div>
  <div class="addMemberBox">
    <!-- <div class="askQuestionsUpload">
      <div class="askQuestionsUploadAdd">
        <input type="file" id="uploadAskImage" />
        <i class="fa fa-plus fa-lg"></i>
        <p>上传角色照片</p>
        <img id="previewPhoto" style="display:none;" />
      </div>
    </div> -->
    <div class="askQuestionsInput">
      <input type="text" id="m_name" placeholder="请输入游戏角色" />
    </div>
    <div class="askQuestionsInput">
      <select type="text" id="m_profession">
        <option value="99">--请选择游戏职业--</option>
        <option value="0">战士</option>
        <option value="4">法师</option>
        <option value="5">术士</option>
        <option value="6">德鲁伊</option>
        <option value="7">圣骑士</option>
        <option value="3">牧师</option>
        <option value="2">盗贼</option>
        <option value="1">猎人</option>
      </select>
    </div>
    <div class="askQuestionsSubmit">
      <div class="askQuestionsBtn">新增成员</div>
    </div>
  </div>
  <div class="memberManagerTitle" style="margin-top:20px;border-bottom:1px solid #ccc;padding-bottom:10px;">
    成员列表<b>（当前总人数：<%- memberData.length %>）</b>
    <input type="date" id="nowDate" value="" style="height:30px;position:relative;top:-1px;width:150px;" />
    <div style="float:right;">
      <input type="text" id="dkpValue" placeholder="请输入分数"
        style="width:150px;height:30px;padding:5px;position:relative;top:-1px;">
      &nbsp;&nbsp;
      <button type="button" class="btn btn-primary btn-sm submitActionDkp" data-action="0" >新增 DKP</button>
      &nbsp;&nbsp;
      <button type="button" class="btn btn-primary btn-sm submitActionDkp" data-action="1" >扣除 DKP</button>
      &nbsp;&nbsp;/&nbsp;&nbsp;
      <button type="button" class="btn btn-info btn-sm" id="allCheck">选中全部</button>
      &nbsp;&nbsp;
      <button type="button" class="btn btn-info btn-sm" id="allCheckCancel">全部取消</button>
    </div>

  </div>
  <div class="memberIndexBox">
    <div class="memberItemBox">
      <% for(var i=0;i<memberData.length;i++){ %>
      <div class="memberItem">
        <div class="removeMemberAction" id="<%- memberData[i].m_id %>">
          <i class="fa fa-remove fa-lg"></i>
        </div>
        <!-- <div class="memberPhoto">
          <% if(memberData[i].m_photo===null || memberData[i].m_photo == ""){ %>
          <img src="/image/dkp_13.png" />
          <% }else{ %>
          <img src="<%- memberData[i].m_photo %>" />
          <% } %>
        </div> -->
        <div class="checkbox" style="float:left;position:relative;left:10px;top:-3px;">
          <label>
            <input class="memberCheck" value="<%- memberData[i].m_id %>" type="checkbox" style="width:20px;height:20px;">
          </label>
        </div>
        <div class="memberName">
          <% if(memberData[i].m_profession == 0){ %>
          <img src="/image/dkp_6.png" />
          <% }else if(memberData[i].m_profession == 1){ %>
          <img src="/image/dkp_7.png" />
          <% }else if(memberData[i].m_profession == 2){ %>
          <img src="/image/dkp_8.png" />
          <% }else if(memberData[i].m_profession == 3){ %>
          <img src="/image/dkp_9.png" />
          <% }else if(memberData[i].m_profession == 4){ %>
          <img src="/image/dkp_10.png" />
          <% }else if(memberData[i].m_profession == 5){ %>
          <img src="/image/dkp_11.png" />
          <% }else if(memberData[i].m_profession == 6){ %>
          <img src="/image/dkp_12.png" />
          <% }else if(memberData[i].m_profession == 7){ %>
          <img src="/image/dkp_16.png" />
          <% } %>
          <%- memberData[i].m_name %></div>
        <div>
          <div class="form-group"><input type="text" class="deductDkpEquipment_<%- memberData[i].m_id %>"  placeholder="请输入物品名称"
              style="width:90%;height:30px;margin-left:5%;padding:5px;"></div>
        </div>
        <div>
          <div class="form-group"><input type="text" class="deductDkpValue_<%- memberData[i].m_id %>" placeholder="请输入扣除分数"
              style="width:90%;height:30px;margin-left:5%;padding:5px;"></div>
        </div>
        <div>
          <div class="form-group"><button type="submit" style="width:90%;height:35px;margin-left:5%;"
              class="btn btn-primary form-control deductDkpBtn" id="<%- memberData[i].m_id %>">提交</button></div>
        </div>
        <div style="height:1px;width:100%;background:#ccc;"></div>
        <div style="height:40px;width:100%;text-align:center;line-height:40px;">当前分数：<b
            style="color:green;letter-spacing: 2px;"><%- memberData[i].m_integral %>分</b></div>
      </div>

      <% } %>

    </div>
  </div>
</body>

</html>
<script>

  ///dkpAction/deductDkp

  var teamValue = customStorage.getPersistenceValue("teamValue");
  var teamName = teamValue == 1 ? '黑翼团' : '安琪拉团';
  $("#nowTeam").html(teamName);

  //照片地址
  var photoUrl = "";

  $("#previewPhoto").click(function () {

    $("#uploadAskImage").click();

  });

  //初始化日期值
  var dateValue =  getQueryString("dateValue")
  if(dateValue!=null){
    $("#nowDate").val(dateValue)
  }

  $(".deductDkpBtn").click(function(){

    var mid = $(this).attr("id");

    var dkpName = $(".deductDkpEquipment_"+mid).val();
    var dkpValue = $(".deductDkpValue_"+mid).val();

    if(str_trim(dkpName).length == 0 || str_trim(dkpValue).length == 0){
      POP.error("提示信息", "请先输入装备名称和扣除的DKP数量");
      return;
    }

    var nowDate = $("#nowDate").val();
    if (nowDate.length == 0) {
      POP.error("提示信息", "请选择日期");
      return;
    }

    if(!isRealNum(dkpValue)){
      POP.error("提示信息", "DKP值必须为数字");
      return;
    }

    
    var params = {
      nowDate : nowDate,
      equipmentName : dkpName,
      dkpValue : dkpValue,
      mId : mid
    }


    POP.confirm("操作提示", "确定要进行该操作吗？", function () {

      POP.startLoading();
      HTTP.post("/dkpAction/deductDkp", params, (rs) => {

        POP.endLoading();
        POP.success("提示信息", "DKP装备记录扣除成功");

        setTimeout(function(){
          location.href="?dateValue="+nowDate;
        },1000);
        
      })
    });


  })

  $(".submitActionDkp").click(function () {

    var actionType = $(this).data("action");

    //获取日期
    var nowDate = $("#nowDate").val();
    if (nowDate.length == 0) {
      POP.error("提示信息", "请选择日期");
      return;
    }

    //获取新增分数
    var dkpValue = $("#dkpValue").val();
    if(str_trim(dkpValue).length == 0 || dkpValue <= 0){
      POP.error("提示信息", "请输入要新增的DKP分数");
      return;
    }

    if(!isRealNum(dkpValue)){
      POP.error("提示信息", "DKP值必须为数字");
      return;
    }
    

    //获取选中的人
    var memberArr = [];
    $('.memberCheck').each(function() { 
      if ($(this).prop("checked") == true) { 
          memberArr.push($(this).val()); 
      } 
    }); 

    if(memberArr.length == 0){
      POP.error("提示信息", "请选择需要操作的成员");
      return;
    }

    var params = {
      nowDate : nowDate,
      dkpValue : dkpValue,
      memberArr : memberArr,
      actionType : actionType //0-新增 1-扣除
    }


    POP.confirm("操作提示", "确定要进行该操作吗？", function () {

      POP.startLoading();
      HTTP.post("/dkpAction/addDkp", params, (rs) => {

        POP.endLoading();
        POP.success("提示信息", "DKP新增成功");

        setTimeout(function(){

          location.href="?dateValue="+nowDate;

        },1000);
        

      })


    });

    

  })


  //照片上传
  $("#uploadAskImage").change(function (e) {

    var file = e.target.files[0];
    if (file == undefined) { return }

    //判断文件类型
    var fileExt = file.name.toLowerCase().split('.').splice(-1);
    var isExt = containsArr(['jpg', 'png', 'gif', 'svg', 'jpeg'], fileExt[0]);
    if (!isExt) {
      return cb(false, "抱歉,不支持上传该格式的文件");
    }

    POP.startLoading();

    //获取上传签名
    HTTP.post("/common/getTSTToken", {}, (rs) => {

      var client = new OSS.Wrapper({
        region: "oss-cn-beijing",
        accessKeyId: rs.AccessKeyId,
        accessKeySecret: rs.AccessKeySecret,
        stsToken: rs.SecurityToken,
        bucket: "ufo-dkp"
      });

      var fileName = Date.parse(new Date());
      var fileExt = file.name.toLowerCase().split('.').splice(-1);
      var randStr = randomString(6);
      var newFilename = fileName + "-" + randStr + "." + fileExt[0];

      client.multipartUpload("/" + newFilename, file).then(function (result) {

        POP.endLoading();

        var imgUrl = "";

        if (result.url != undefined) {

          imgUrl = result.url;

        } else {

          var IMAGE_SERVER = "https://ufo-dkp.oss-cn-beijing.aliyuncs.com";
          imgUrl = IMAGE_SERVER + result.name;
        }

        //重新生成预览图
        console.log(imgUrl)
        $("#previewPhoto").attr("src", imgUrl);
        $("#previewPhoto").show();

        photoUrl = imgUrl;


      }).catch(function (err) {
        console.log(err);
        POP.error("图片上传失败，请重新尝试")
      });

    })

  });

  //新增成员处理
  $(".askQuestionsBtn").click(function () {

    var m_name = $("#m_name").val();
    var m_profession = $("#m_profession").val();

    if (str_trim(m_name).length <= 0) {
      POP.warning("验证提示", "角色名称不能为空");
      return;
    }

    if (m_profession == 99) {
      POP.warning("验证提示", "角色职业不能为空");
      return;
    }

    var params = {
      m_name: m_name,
      m_profession: m_profession,
      m_tid: teamValue,
      m_photo: photoUrl
    }

    POP.startLoading();

    HTTP.post("/dkpAction/addMember", params, (rs) => {

      POP.endLoading();
      POP.success("提示信息", "成员新增成功");



      setTimeout(() => {

        // //清空内容
        $("#m_name").val("");
        $("#m_profession option[value='99']").prop("selected", true);

        location.reload();

      }, 1500);

    })
  });

  //删除成员
  $(".removeMemberAction").click(function () {

    var _this = $(this);

    POP.confirm("删除提示", "确定要删除该成员吗？", function () {

      var params = {
        m_id: _this.attr("id")
      }

      POP.startLoading();

      HTTP.post("/dkpAction/removeMember", params, (rs) => {

        POP.endLoading();
        POP.success("提示信息", "成员删除成功");
        location.reload();

      })


    });
  });

  $("#allCheck").click(function () {
    $(".memberCheck").prop("checked", true);
  })

  $("#allCheckCancel").click(function () {
    $(".memberCheck").prop("checked", false);
  })



</script>